<?php


namespace app\common\classes;


use think\facade\Env;

/**
 * 行为记录
 * Class VoucherRedis
 * @package app\common\classes
 */
class BehaviorRedis
{
    protected $redis;
    protected $host;
    protected $port;
    protected $auth;

    public function __construct()
    {
        $this->host = Env::get('redis.host', 'r-bp10ft1t8un60tk4bb.redis.rds.aliyuncs.com');
        $this->port = 6379;
        $this->auth = Env::get('redis.auth', 'r-bp11v3wc1977f8zhaj:dsfweew%$weew2');
    }

    /**
     * 获取reids对象
     * @return \Redis
     */
    public function getRedis()
    {
        if (!is_object($this->redis)) {
            $this->redis = new \Redis();
            $this->redis->connect($this->host, $this->port);
            $this->redis->auth($this->auth);
        }
        return $this->redis;
    }

    /**
     * 添加弹窗记录
     * @param $popId
     * @param $frequency
     * @param $uid
     * @param $expire
     */
    public function addPopLog($popId, $frequency, $uid, $expire)
    {
        $key = "pop_{$popId}_{$frequency}_{$uid}";
        $this->getRedis()->set($key, time());
        $this->getRedis()->expire($key, $expire);
    }

    /**
     * 获取弹窗记录
     * @param $popId
     * @param $frequency
     * @param $uid
     * @return bool|mixed|string
     */
    public function getPopLog($popId, $frequency, $uid)
    {
        $key = "pop_{$popId}_{$frequency}_{$uid}";
        return $this->getRedis()->get($key);
    }

    /**
     * 正在阅读行为记录
     * @param $uid
     * @param $novelId
     */
    public function addReading($uid, $novelId)
    {
        $suffix = date("Y_m_d_H_i");
        $key1 = "reading_users_" . $suffix;
        $key2 = "reading_novels_" . $suffix;
        $key3 = "reading_users_{$novelId}_" . $suffix;
        $this->getRedis()->sAdd($key1, $uid);
        $this->getRedis()->sAdd($key2, $novelId);
        $this->getRedis()->sAdd($key3, $uid);
        $expire = 300;
        $this->getRedis()->expire($key1, $expire);
        $this->getRedis()->expire($key2, $expire);
        $this->getRedis()->expire($key3, $expire);
    }

    /**
     * 获取总在读人数
     * @return int
     */
    public function readingUsersCount()
    {
        $suffix = date("Y_m_d_H_i");
        $key = "reading_users_" . $suffix;
        return $this->getRedis()->sCard($key);
    }

    /**
     * 获取单本书在读人数
     * @param $novelId
     * @return int
     */
    public function readingUsersNovelCount($novelId)
    {
        $suffix = date("Y_m_d_H_i");
        $key = "reading_users_{$novelId}_" . $suffix;
        return $this->getRedis()->sCard($key);
    }


    /**
     * 在读书籍
     * @return int
     */
    public function readingNovels()
    {
        $suffix = date("Y_m_d_H_i");
        $key = "reading_novels_" . $suffix;
        return $this->getRedis()->sCard($key);
    }

    /**
     * 获取ip归属地
     * @param $ip
     * @return bool|mixed|string
     */
    public function getIpAttributionCache($ip)
    {
        $key = "ip_" . $ip;
        return $this->getRedis()->get($key);
    }


    /**
     * 设置ip归属地缓存
     * @param $ip
     * @param $attribution
     */
    public function setIpAttributionCache($ip, $attribution)
    {
        $key = "ip_" . $ip;
        $this->getRedis()->set($key, $attribution);
        $this->getRedis()->expire($key, 432000);//保留5天
    }

    /**
     * 链接uv
     * @param $linkId
     * @param $uid
     */
    public function setLinkUv($linkId, $uid)
    {
        $key = "link_uv_{$linkId}_" . date("Y-m-d");
        $this->getRedis()->sAdd($key, $uid);
        $this->getRedis()->expire($key, 432000);//保留5天
    }

    /**
     * 获取链接uv数量
     * @param $linkId
     * @param $date
     * @return int
     */
    public function getLinkUvCount($linkId, $date)
    {
        $key = "link_uv_{$linkId}_" . $date;
        return $this->getRedis()->sCard($key);
    }


    /**
     * 记录小说每日uv
     * @param $qid
     * @param $bookId
     * @param $date
     * @param $userId
     */
    function addBookUv($qid, $bookId, $date, $userId)
    {
        //分快应用
        $Key = "book_uv_{$qid}_{$date}:" . $bookId;
        $this->getRedis()->sAdd($Key, $userId);
        $timeout = 259200;
        $this->getRedis()->expire($Key, $timeout);

        //记总的
        $Key2 = "book_uv_all_{$date}:" . $bookId;
        $this->getRedis()->sAdd($Key2, $userId);
        $timeout = 259200;
        $this->getRedis()->expire($Key2, $timeout);
    }

    /**
     * 获取小说每日uv（分快应用）
     * @param $qid
     * @param $bookId
     * @param $date
     * @return int
     */
    function getBookUvCount($qid, $bookId, $date)
    {
        $Key = "book_uv_{$qid}_{$date}:" . $bookId;
        return $this->getRedis()->sCard($Key);
    }

    /**
     * 获取小说每日uv（总的）
     * @param $bookId
     * @param $date
     * @return int
     */
    function getAllBookUvCount($bookId, $date)
    {
        $Key = "book_uv_all_{$date}:" . $bookId;
        return $this->getRedis()->sCard($Key);
    }
}